<?php
class folder_item extends Model{
	public static function getTableName(){
		return 'folder_item';
	}

	/*
	 * by donald
	 */
    public function getPrimaryKey()
    {
        return array('folder_id', 'item_id');
    }

	public function getSelectedItem($folderid)
	{
		$sql = 'select * from '.self::getTableName().',folder where folder_id="'.$folderid.'"';
		return Model::$dbo['r']->findBySql(__class__,$sql);
	}

	public function deleteByFolder($folderid)
	{
		return Model::$dbo['w']->delete(__class__, " folder_id ='?'", array($folderid));
	}

	public function getMaxRank($folderid){
		$max_rank = Model::selectBySql('select max(rank) as maxrank from folder_item where folder_id = :id',array(':id'=>$folderid));
		$max_rank = $max_rank[0]['maxrank'];
		return $max_rank;
	}

	/**
	 * get catch up videos
	 */
	public static function getCatchUpVideos($ids)
	{
		$ids = is_null($ids) ? '' : $ids;
        $sql = '
            select * from video 
            where id in 
            (
                select a.item_id from folder_item a,folder b 
                where b.id in ('.$ids.') and b.folder_type_id='.AC::VIDEO_CATCHUP.' and a.folder_id=b.id
            )
            ';
		$ret = Model::$dbo['r']->findBySql(__class__,$sql);
		return isset($ret[0]) ? $ret : array();
	}

	public static function getCatchUpPremiumVideos($ids)
	{
		$ids = is_null($ids) ? '' : $ids;
        $sql = '
            SELECT * FROM `video` 
            WHERE premium_flag = 1 AND id IN 
            (
                SELECT `folder_item`.item_id 
                FROM `folder_item`, `folder` 
                WHERE `folder`.id in ('.$ids.') and `folder`.folder_type_id='.AC::VIDEO_CATCHUP.' and `folder_item`.folder_id=`folder`.id
            )
            ';
		$ret = Model::$dbo['r']->findBySql(__class__, $sql);
		return isset($ret[0]) ? $ret : array();
	}

    public function getItemByFolderId($folder_id)/*{{{*/
    {
        $ret = array();

        if ($folder_id) {
            $sql = "SELECT `item_id` FROM `folder_item` WHERE `folder_id`=:folder_id";
            $result = Model::selectBySql($sql, array(':folder_id' => $folder_id));
            if (!empty($result)) {
                foreach ($result as $row) {
                    $ret[] = $row['item_id'];
                }
            }
        }

        return $ret;
    }/*}}}*/

	/*
	 * by donald
	 * modified at 2011-09-22
	 */
	public static function getAll(array $params)
	{
		foreach ($params as $field => $value) {
			switch ($field) {
				case 'folder_id':
					$where[] = 'folder_id = ?';
					$where_array[] = $value;
					break;
				case 'item_id':
					$where[] = 'item_id = ?';
					$where_array[] = $value;
					break;
				default:
					break;
			}
		}

		if (empty($params['type'])) {
			return self::$dbo['w']->find(__CLASS__, implode(' AND ', $where), $where_array);
		} 

		switch (strtolower($params['type'])) {
			case 'image':
				$folder_item_type_id = AC::FOLDER_TYPE_IMAGE;
				break;
			case 'video':
				$folder_item_type_id = AC::FOLDER_TYPE_VIDEO;
				break;
			default:
				return array();
				break;
		}

		$folder_item_list = self::$dbo['w']->find(__CLASS__, implode(' AND ', $where), $where_array);
		foreach ($folder_item_list as $key => $folder_item_detail) {
			$folder_detail = folder::getAll(
				array(
					'id' => $folder_item_detail->folder_id , 
					'folder_item_type_id' => $folder_item_type_id
				)
			);

			//filter type
			if (empty($folder_detail)) {
				unset($folder_item_list[$key]);
			}
		}

		return $folder_item_list;
	}

	/*
	 * by donald
	 * at 2011-09-22
	 */
	public function add_change_log($programme_id, $episode_id, $is_active)
	{
		$field['type'] = $this->getTableName();
		$field['keys'] = array(
			'folder_id' => $this->folder_id,
			'item_id' => $this->item_id
		);
		$field['is_active'] = $is_active; 
		$field['programme_id'] = $programme_id;
		$field['episode_id'] = $episode_id; 
		$field['is_publish'] = 0;

		change_log::updateOne($field);
	}

	/*
	 * by donald
	 */
	public function delete_change_log()
	{
		$type = $this->getTableName();
		$keys = array(
			'folder_id' => $this->folder_id,
			'item_id' => $this->item_id
		);
		$is_publish = 0;

		change_log::delTypeAndKeysLog($type, $keys, $is_publish);
	}

	/*
	 * by donald
	 * at 2011-09-22
	 * $type: video | image
	 * $item_id: video_id | image_id
	 */
	public static function addFolderItemChangeLog($type, $item_id, $programme_id, $episode_id = 0, $is_active = 1)
	{
		$folder_item_list = folder_item::getAll(
			array(
				'type' => $type,
				'item_id' => $item_id
			)
		);

		foreach ($folder_item_list as $folder_item) {
			$log_programme_id = $programme_id;
			$log_episode_id = $episode_id;
			if ($type == 'image' && $programme_id) {
				$log_programme_id = programme::getProgrammeIdForChangeLog($folder_item->folder_id, $programme_id);
				$log_episode_id = $log_programme_id ? 0 : episode::getEpisodeIdForChangeLog($folder_item->folder_id, $episode_id); 
			}
			$folder_item->add_change_log($log_programme_id, $log_episode_id, $is_active);
			folder_item_language::addFolderItemLanguageChangeLog($folder_item->folder_id, $folder_item->item_id, $log_programme_id, $log_episode_id, $is_active);
		}
	}
}
?>
